package com.ch.ms.server.login.service.impl;

import com.ch.ms.data.domain.ChmsServer;
import com.ch.ms.service.ChmsServerCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ChmsServerCacheServiceImpl implements ChmsServerCacheService {


    private HashOperations<String, String, ChmsServer> chmsServerHashOperations;

    @Autowired
    public ChmsServerCacheServiceImpl(RedisTemplate<String, ChmsServer> chmsServerRedisTemplate) {
        chmsServerHashOperations = chmsServerRedisTemplate.opsForHash();
    }


    @Override
    public ChmsServer save(ChmsServer entity) {
        if (Objects.isNull(entity)) return null;
        chmsServerHashOperations.put(getKey(entity), entity.getUuid(), entity);
        return entity;
    }

    @Override
    public void saveAll(Iterable<ChmsServer> entities) {
        if (Objects.nonNull(entities))
            entities.forEach(this::save);
    }

}
